package com.etc.myblog.biz;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;



import com.etc.myblog.dao.ArticleDao;
import com.etc.myblog.dao.UserDao;
import com.etc.myblog.entity.Article;
import com.etc.myblog.util.SessionFactoryUtil;

public class ArticleBiz {
	
	//获取所有的文章
	public List<Article> find() {
		List<Article> list = null;
		// 取SqlSession对象,通过SqlSession 对象就可以操作数据库中的数据
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class); // 映射数据访问接口
			list = dao.find(); // 调用数据访问接口的方法，由MyBatis返回映射回来的数据集合。
			for(Article ar : list)
			System.out.println(ar.getArticleStatus());
		    //list = session.selectList("findStudent",Student.class);
		} catch (Exception e) {
			System.out.println("有错误：" + e.getMessage());
		} finally {
			session.close();  //关闭连接
		}
		return list;
	}
	//添加文章
	public void insertArticle(Article article) {
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			dao.insertArticle(article);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
	}
	//根据用户id获取文章信息
	public List<Article> findByUserId(int userId) {
		List<Article> list = new ArrayList<Article>();
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			list = dao.findByUserId(userId);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		return list;
	}
	//根据文章id获取文章信息
	public Article findById(int articleId) {
		Article article = new Article();
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			article = dao.findById(articleId);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		return article;
	}
	
	//根据用户id获取10个相关文章
	public List<Article> findByUserIdTen(int userId) {
		List<Article> list = new ArrayList<Article>();
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			list = dao.findByUserIdTen(userId);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		return list;
	}
	//根据文章id删除文章
	public void delete(int articleId) {
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			dao.delete(articleId);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
	}
	//根据内容更新文章
	public void update(Article article) {
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			dao.update(article);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
	}
	//根据专栏id获取文章
	public List<Article> findByColumn(int columnId) {
		List<Article> list = new ArrayList<Article>();
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			list = dao.findByColumn(columnId);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		return list;
	}
	
	//获取点击量前10的文章
	public List<Article> findClickTop() {
		List<Article> list = new ArrayList<Article>();
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			list = dao.findClickTop();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		return list;
	}
	
	//获取用户的文章数目
	public int articleCount(int userId) {
		int count = 0;
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			count = dao.articleCount(userId);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		return count;
	}
	
	//获取用户的文章总浏览次数
	public int articleView(int userId) {
		int sum = 0;
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			sum = dao.articleView(userId);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		return sum;
	}
	
	//获取点击量周排行前8的文章
	public List<Article> findArtTopWeek() {
		List<Article> list = new ArrayList<Article>();
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			list = dao.findArtTopWeek();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
		return list;
	}
	//设置点击量
	public void setArtClick(int articleId) {
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			dao.setArtClick(articleId);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
	}
	
	public List<Article> findNoAdArticle() {
		List<Article> list = null;
		// 取SqlSession对象,通过SqlSession 对象就可以操作数据库中的数据
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class); // 映射数据访问接口
			list = dao.findNoAdArticle(); // 调用数据访问接口的方法，由MyBatis返回映射回来的数据集合。
		} catch (Exception e) {
			System.out.println("有错误：" + e.getMessage());
		} finally {
			session.close();  //关闭连接
		}
		return list;
	}
	
	public Article findByArticleId(int articleId) {
		Article ar = null;
		// 取SqlSession对象,通过SqlSession 对象就可以操作数据库中的数据
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class); // 映射数据访问接口
			ar = dao.findByArticleId(articleId); // 调用数据访问接口的方法，由MyBatis返回映射回来的数据集合。
		} catch (Exception e) {
			System.out.println("有错误：" + e.getMessage());
		} finally {
			session.close();  //关闭连接
		}
		return ar;
	}
	
	public void updateStatus(Article article){
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao  dao = session.getMapper(ArticleDao.class); // 映射数据访问接口
			dao.updateStatus(article);
			session.commit();
		} catch (Exception e) {
			System.out.println("有错误：" + e.getMessage());
		} finally {
			session.close(); // 关闭连接
		}
	}
	
	public List<Article> findNoAdArticleBysortId(int sortId) {
		
		List<Article> list = new ArrayList<Article>();
		// 取SqlSession对象,通过SqlSession 对象就可以操作数据库中的数据
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class); // 映射数据访问接口
			list = dao.findNoAdArticleBysortId(sortId); // 调用数据访问接口的方法，由MyBatis返回映射回来的数据集合。
		} catch (Exception e) {
			System.out.println("有错误：" + e.getMessage());
		} finally {
			session.close();  //关闭连接
		}
		return list;
	}
	//随机一个文章
	public Article findOne() {
		Article ar = new Article();
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class); // 映射数据访问接口
			ar = dao.findOne(); // 调用数据访问接口的方法，由MyBatis返回映射回来的数据集合。
		} catch (Exception e) {
			System.out.println("有错误：" + e.getMessage());
		} finally {
			session.close();  //关闭连接
		}
		return ar;
	}
	
	public void addNoCol(Article article) {
		SqlSession session = SessionFactoryUtil.getSession();
		try {
			ArticleDao dao = session.getMapper(ArticleDao.class);
			dao.addNoCol(article);
			session.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			session.rollback();
		} finally {
			session.close();
		}
	}

}
